package com.bidevalution.controller.task;

import com.bidevalution.business.service.BeChangeProjectHistoryService;
import com.bidevalution.dao.entity.BeChangeProjectHistory;
import com.bidevalution.support.context.Constants;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 变更项目id定时任务
 * @Date        2020-04-28 10:50
 * @Author      lhl
 */
@Log4j2
@Component
public class ChangeProjectIdTask {

    @Autowired
    private BeChangeProjectHistoryService beChangeProjectHistoryService;

    /**
     * 容错次数
     */
    private static final int FAULT_TOLERANT_NUM = 3;

    /**
     * @Description 变更项目id定时任务 每天0时触发
     * @date 2020-04-28 11:22
     * @author lhl
     */
//    @Scheduled(cron = "0 0 0 * * ?")
    @Scheduled(cron = "0 * * * * ?")
    public void extract(){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("state", Constants.CHANGE_PROJECT_ID_PENDING);
        params.put("changeNum",FAULT_TOLERANT_NUM);
        for (BeChangeProjectHistory item : beChangeProjectHistoryService.getPendingList(params)) {
            try {
                item.setChangenum(item.getChangenum()+1);
                beChangeProjectHistoryService.updateByPrimaryKeySelective(item);
                beChangeProjectHistoryService.executeDisposeChangeProjectId(item);
            }catch (Exception e){
                log.error("LOG[]extract_41[]{},error={},item={}","变更项目id定时任务异常日志", GsonUtil.toJsonWithGson(e), GsonUtil.toJsonWithGson(item));
            }
        }
    }

}
